package com.ysxsoft.wdg.ui.activity.my.jf;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.classic.common.MultipleStatusView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.ysxsoft.wdg.R;
import com.ysxsoft.wdg.adapter.my.JfGoodsAdapter;
import com.ysxsoft.wdg.base.BaseActivity;
import com.ysxsoft.wdg.bean.BaseBean;
import com.ysxsoft.wdg.bean.MyJfBean;
import com.ysxsoft.wdg.net.CallbackCode;
import com.ysxsoft.wdg.util.ToastUtils;
import com.ysxsoft.wdg.util.sp.SharePrefUtils;
import com.ysxsoft.wdg.util.statusbar.StatusBarUtil;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class MyJfActivity extends BaseActivity implements OnRefreshListener{


    private String uid;

    public static void start(Context context){
        Intent intent = new Intent(context,MyJfActivity.class);
        context.startActivity(intent);
    }

    @BindView(R.id.topView)
    View topView;
    @BindView(R.id.title_finish)
    ImageView titleFinish;
    @BindView(R.id.title_content)
    TextView titleContent;
    @BindView(R.id.title_tv_r)
    TextView titleTvR;
    @BindView(R.id.tv_jf)
    TextView tvJf;
    @BindView(R.id.tv_sign_days)
    TextView tvSignDays;  @BindView(R.id.btn_sign)
    TextView btn_sign;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.multStatusView)
    MultipleStatusView multStatusView;


    private JfGoodsAdapter mAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_jf;
    }

    @Override
    protected void initData() {
        StatusBarUtil.setStatusBarTextColor(this,true);
        initStatusBar(topView,true);
        titleContent.setText("我的积分");
        titleTvR.setText("积分说明");

        mAdapter = new JfGoodsAdapter(R.layout.item_jf_goods);
        recyclerview.setNestedScrollingEnabled(false);
        recyclerview.setFocusable(false);
        recyclerview.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
        recyclerview.setAdapter(mAdapter);

        uid = SharePrefUtils.getUserId();

        multStatusView.showLoading();

    }

    @Override
    protected void onResume() {
        super.onResume();
        getInfo();
    }

    private void getInfo() {
        mApiHelper.getJfInfo(uid, new Observer<BaseBean<MyJfBean>>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(BaseBean<MyJfBean> baseBean) {
                multStatusView.showContent();
                if (baseBean.getCode().equals(CallbackCode.SUCCESS)){

                    MyJfBean data = baseBean.getData();
                    MyJfBean.InfoBean info = data.getInfo();

                    tvJf.setText(info.getScore());
                    tvSignDays.setText("已连续签到"+info.getDay()+"天");

                    mAdapter.setNewData(data.getShop());

                    if("1".equals(info.getIssign())){
                        btn_sign.setText("今日已签到");
                    }
                }
            }

            @Override
            public void onError(Throwable e) {
                multStatusView.showError();
            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    protected void setListener() {
        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setEnableLoadmore(false);

        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                JfGoodsDetailsActivity.start(mContext,mAdapter.getItem(position).getGoodsid());
            }
        });
    }


    @OnClick({R.id.title_finish, R.id.title_tv_r,
            R.id.btn_sign, R.id.tv_wddh, R.id.tv_jfmx})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_finish:
                finish();
                break;
            case R.id.title_tv_r://积分说明
                JfDescActivity.start(mContext);
                break;
            case R.id.btn_sign://签到
                postSign();
                break;
            case R.id.tv_wddh://积分兑换
                MyJfdhActivity.start(mContext);
                break;
            case R.id.tv_jfmx://积分明细
                JfmxActivity.start(mContext);
                break;
        }
    }

    private void postSign() {
        mApiHelper.signUp(uid, new Observer<BaseBean>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(BaseBean baseBean) {
                ToastUtils.showToast(baseBean.getMs());
                if (baseBean.getCode().equals(CallbackCode.SUCCESS)){
                    onRefresh(refreshLayout);
                }
            }

            @Override
            public void onError(Throwable e) {
                ToastUtils.showToast("请稍后再试");
            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        refreshlayout.finishRefresh(500);
        getInfo();
    }
}
